# python 3.6.8 
import logging
import re
from urllib.parse import unquote

logging.basicConfig(filename = 'log.log', level = logging.DEBUG, format = ' %(asctime)s - %(levelname)s - %(message)s')
#logging.disable(logging.CRITICAL)

def to_chinese(s):
    ch = s.group('value').split('\\')
    result = ''
    logging.debug(f'将url编码的汉字进行还原: {ch}')
    for i in ch:
        if '' == i:
            continue
        logging.debug(f'转化为16进制: {i}')
        result += '%' + str(hex(eval('0o' + i)))[2:]
    return unquote(result)

def oct_decode(s):
    return re.sub(r'(?P<value>(\\[0-7]{3}){3})', to_chinese, s)

if __name__ == '__main__':
    import sys
    sys.path.append('../')
    from utils.File import readFile, writeFile
    cont = readFile('f1.txt')
    cont = oct_decode(cont)
    writeFile('f2.txt', cont)
